package cn.yunhe.controller;

import cn.yunhe.model.Page;
import cn.yunhe.model.Person;
import cn.yunhe.service.impl.PersonServiceImpl;
import cn.yunhe.view.ExcelExportService;
import cn.yunhe.view.ExcelView;
import cn.yunhe.view.FileUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author Created by zs on 2017/12/20.
 */

@Controller
@RequestMapping("/person")
public class PersonController {


    @Resource
    private PersonServiceImpl personServiceImpl;


    @RequestMapping("/getLikePerson")
    public ModelAndView queryLikePerson(Person person, int pageNum, int pageSize){
        ModelAndView mv = new ModelAndView();
        Page page = null;
        Map<String, Object> cond = new HashMap<String, Object>();
        cond.put("pageSize", pageSize);
        cond.put("pageNum", pageNum);
        cond.put("person", person);
        page = personServiceImpl.queryLikePersons(cond);
        int letAge = personServiceImpl.selAgePerson(person);
        int betAge = personServiceImpl.seleAgePerson(person);
        mv.setView(new MappingJackson2JsonView());
        mv.addObject("page", page);
        mv.addObject("letAge", letAge);
        mv.addObject("betAge", betAge);
        return mv;

    }

    @RequestMapping("/addPerson")
    public ModelAndView addPerson(Person person){
        ModelAndView mv = new ModelAndView();
        personServiceImpl.addPerson(person);
        mv.setView(new MappingJackson2JsonView());
        return mv;
    }

    @RequestMapping("/delPerson")
    public ModelAndView delPerson(Person person){
        ModelAndView mv = new ModelAndView();
        personServiceImpl.delPerson(person);
        mv.setView(new MappingJackson2JsonView());
        return mv;
    }

    @RequestMapping("/findPerson")
    public String findPerson(Model model, Person person){
         Person per=null;
         per= personServiceImpl.findPerson(person);
         model.addAttribute("person",per);
        return "forward:/edit_person.jsp";
    }

    @RequestMapping("/editPerson")
    public ModelAndView editPerson(Person person){
        ModelAndView mv = new ModelAndView();
        personServiceImpl.updatePerson(person);
        mv.setView(new MappingJackson2JsonView());
        return mv;
    }

    @RequestMapping("export")
    public void export(HttpServletResponse response) throws Exception {

        //模拟从数据库获取需要导出的数据
        List<Person> roleList =  personServiceImpl.getAllPersons();
        //导出操作
        FileUtil.exportExcel(roleList,"花名册","草帽一伙",Person.class,"海贼王.xls",response);
    }



    /*@RequestMapping(value = "/export", method = RequestMethod.GET)
    public ModelAndView export() throws UnsupportedEncodingException {
        //模型和视图
        ModelAndView mv = new ModelAndView();
        //Excel视图，并设置自定义导出接口
        ExcelView ev = new ExcelView(exportService());
        //文件名
        String filename = "所有角色.xls";
        String filename2 = URLEncoder.encode(filename, "utf-8");
        ev.setFileName(filename2);
        List<Person> roleList =  personServiceImpl.getAllPersons();
        //加入数据模型
        mv.addObject("roleList", roleList);
        mv.setView(ev);
        return mv;
    }*/

    @SuppressWarnings({ "unchecked"})
   /* private ExcelExportService exportService() {
        //使用Lambda表达式自定义导出excel规则
        return (Map<String, Object> model, Workbook workbook) -> {
            //获取用户列表
            List<Person> roleList = (List<Person>) model.get("roleList");
            //生成Sheet
            Sheet sheet= workbook.createSheet("所有角色");
            //加载标题
            Row title = sheet.createRow(0);
            title.createCell(0).setCellValue("序号");
            title.createCell(1).setCellValue("用户");
            title.createCell(2).setCellValue("年龄");

            //便利角色列表，生成一行行的数据
            for (int i=0; i<roleList.size(); i++) {
                Person role = roleList.get(i);
                int rowIdx = i + 1;
                Row row = sheet.createRow(rowIdx);
                row.createCell(0).setCellValue(role.getId());
                row.createCell(1).setCellValue(role.getName());
                row.createCell(2).setCellValue(role.getAge());
            }
        };
    }*/

    @RequestMapping("/getUnionId")
    public String getUnionId(){
      String re =   personServiceImpl.getUnionId();
      return  re;
    }
    public PersonServiceImpl getPersonServiceImpl() {
        return personServiceImpl;
    }

    public void setPersonServiceImpl(PersonServiceImpl personServiceImpl) {
        this.personServiceImpl = personServiceImpl;
    }




}
